<?php
/*
 *	mid string -> int
 *	新浪接口
 */
function sina_mid2id($mid){
    if(empty($mid)){
        return false;
    }

    $apiUrl="http://api.t.sina.com.cn/queryid.json?mid={$mid}&isBase62=1";
    $rs = file_get_contents($apiUrl);
    $rs = json_decode($rs,true);

    return $rs['id'];
}

/**
 * 高级strtotime
 *
 * $format 可以为timestamp 或者 时间格式
 *
 * @param string $str
 * @param enums $format timestamp/Y-m-d H:i:s
 * @return unknown
 */
function my_strtotime($str, $format='timestamp') {
    //$str = trim(str_replace('&nbsp;', '', $str) );
    $arr = array("&nbsp;","\t","\r","\r\n","\n",'　');
    $str = str_replace($arr, '', $str);
    $str = trim( $str );
    if( empty($str) ) {
        return FALSE;
    }
    $time = 0;
    do
    {
        //第0层  尝试直接strtotime
        $time = strtotime($str);
        if ($time > 0 ) {
            break;
        }

        //第1层
        //处理unix 时间戳形式
        if (is_numeric($str) ) {
            $str = intval($str);
            if( $str > strtotime("2000-01-01") && $str < time()+3600*8 ) {
                $time = $str;
                break;
            }
        }

        //第2层
        //处理 2009年7月23日、 2009年7月23日 12:23
        if( !preg_match("/^(((20)?[01]\d)|(19)?[98]\d)年(\d{1,2})月(\d{1,2})\D*((\d{1,2}):(\d{2})(:(\d{2}))?)?$/", $str, $out) ) {
            //处理  2009.07.23 12:23、 2009.07.23、 2009/07/23
            if ( preg_match("/^(((20)?[01]\d)|(19)?[98]\d)\D(\d{2})\D(\d{2})\D*((\d{1,2}):(\d{2})(:(\d{2}))?(\s?pm)?)?$/i", $str, $out) ) {

            }
        }
        if ( !empty($out) ) {
            //  1-5-6 8:9:11
            //处理可能存在的PM
            $w = trim($out[12]);
            if(!empty($w) && strtolower($w) == 'pm') {
                $out[8] = $out[8] + 12;
            }
            $time = mktime($out[8], $out[9], $out[11], $out[5], $out[6], $out[1]);
            break;
            //$t = strtotime($out[1].'-'.$out[5].'-'.$out[6].' '.intval($out[8]).':'.intval($out[9]).':'.intval($out[11]));
        };


        //第3层  处理不带年份的
        //处理  1-8 、12-25、12-18 3:14 、12-18 3:14 pm形式的时间
        if( preg_match("/^(\d{1,2})[\\\\\/\-\.](\d{1,2})\D?((\d{1,2}):(\d{2})(:(\d{2}))?(\s?pm)?)?$/i", $str, $out) )
        {
            //   1-2 4:5:7
            if( intval($out[1]) >0 && intval($out[1]) <=12
                && intval($out[2]) > 0 && intval($out[2]) <= 31
            ) {
                $year = intval($out[1]) <= date("m") ? date("Y") : date("Y", strtotime("-1 year"));
                $time = mktime(intval($out[4]), intval($out[5]), intval($out[7]), intval($out[1]), intval($out[2]), $year);
                break;
            }
        }

        //第4层： 只有时间的		//处理   3:14、 3:14 pm形式的时间
        if( preg_match("/^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?pm)?$/i", $str, $out) )
        {
            //   1:2:4
            if( intval($out[1]) >0 && intval($out[1]) <=24
                && intval($out[2]) > 0 && intval($out[2]) < 60
            ) {
                $w = trim($out[5]);
                if(!empty($w) && strtolower($w) == 'pm') {
                    $out[1] = $out[1] + 12;
                }

                $time = mktime(intval($out[1]), intval($out[2]), intval($out[4]), date("m"), date("d"), date("Y") );
                break;
            }
        }

        //第5层： //处理 3秒前 形式	 49分钟前  2小时前
        if( preg_match("/^(\d+)\s?(小时|分钟|秒)(以前|前)/", $str, $out) ) {
            $num = intval($out[1]);
            if( $num > 0 && $num < 60 ) {
                if( $out[2] == '小时') {
                    $time = strtotime("-{$num} hours");
                } elseif( $out[2] == '分钟') {
                    $time = strtotime("-{$num} minutes");
                } elseif( $out[2] == '秒') {
                    $time = strtotime("-{$num} seconds");
                }
                break;
            }
        }

        //第6层：//处理1小时25分前这种形式
        if( preg_match("/^(\d+)\s?小时(\d+)分(以前|前)/", $str, $out) ) {
            $hour = intval($out[1]);
            $min = intval($out[2]);
            if( $hour > 0 && $hour < 24 && $min > 0 && $min < 60 ) {
                $exp_time = 60 * $min + 3600 * $hour;
                $time = time() - $exp_time;
                break;
            }
        }

		//第7层 ： 处理 昨天20:30   前天09:20
		if( preg_match("/^(今天|昨天|前天).*?(\d+):(\d+)/", $str, $out) ) {
            $hour = intval($out[2]);
            $min = intval($out[3]);
            if ($hour >= 0 && $hour < 24 && $min >= 0 && $min < 60) {
                $exp_time = 60 * $min + 3600 * $hour;
                if( $out[1] == '昨天' ) {
                    $time = strtotime(date('Y-m-d' , strtotime('-1 day'))) + $exp_time;
                } elseif ($out[1] == '前天' ) {
                    $time = strtotime(date('Y-m-d' , strtotime('-2 days'))) + $exp_time;
                } elseif($out[1] == '今天') {
                    $time = strtotime(date('Y-m-d' , time())) + $exp_time;
                }
                break;
            }
        }

        //第8层 ： 处理  3天前20:30  3 天前 20:38
        if( preg_match("/^(\d+)\s*天前\s*(\d{1,2}:\d{1,2}(:\d{1,2})?)?/", $str, $out) ) {
            $day = $out[1];
            if ( ! empty($out[2]) ) {
                $time = strtotime(date("Y-m-d", time()-3600*24*$day)." ".$out[2]);
            }else {
                $time = strtotime(date("Y-m-d", time()-3600*24*$day)." ".date('H:i:s', time()));
            }
            break;
        }

        //第9层： 处理  07月06日 14:42
        if( preg_match("/^(\d+)月(\d+)日\s*(\d{1,2}:\d{1,2}(:\d{1,2})?)/", $str, $out) ) {
            $month = $out[1];
            $day = $out[2];
            if ($month <= date('m')) {
                $time = strtotime(date("Y").'-'.$month.'-'.$day." ".$out[3]);
                break;
            }
        }

        //第10层： 处理  2010.8.25
        if( preg_match("/^(\d+)\.(\d+)\.(\d+)(\s*(\d{1,2}:\d{1,2}(:\d{1,2})?)?)/", $str, $out) ) {
            $year = $out[1];
            $month = $out[2];
            $day = $out[3];
            $time = strtotime($year.'-'.$month.'-'.$day." ".$out[5]);
            break;
        }

        //add by laurence at Thu May 12 15:24:58 CST 2011
        //第11层： 处理  2011年10月26日 11点43分
        if( preg_match("/^(\d+)\s*年\s*(\d+)\s*月\s*(\d+)\s*日\s*(\d{1,2})(时|点)(\d{1,2})分((\d{1,2})秒?)?/is", $str, $out) ) {

            //print_r($out);exit;
            $year = $out[1];
            $month = $out[2];
            $day = $out[3];
            $hour = $out[4];
            $min = $out[6];
            $sec = '00';
            if(isset($out[8])) {
                $sec = $out[8];
            }

            $isodate = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $hour, $min, $sec);
            $time = strtotime($isodate);
            //$time = strtotime($year.'-'.$month.'-'.$day." {$hour}:{$min}:{$sec}");
            break;
        }

        if (preg_match('|(\d+)\s*年\s*(\d+)\s*月\s*(\d+)\s*日\s*|is', $str, $out)) {
            $year = $out[1];
            $month = $out[2];
            $day = $out[3];
            $hour = $out[4];
            $min = $out[6];
            $sec = '00';
            if(isset($out[8])) {
                $sec = $out[8];
            }
            $isodate = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $hour, $min, $sec);
            $time = strtotime($isodate);
            break;
        }

        //第12层： 处理  5月11日 11点43分
        if( preg_match("/^(\d+)月(\d+)日\s*(\d{1,2})(时|点)(\d{1,2})分((\d{1,2})秒?)?/", $str, $out) ) {
            //print_r($out);exit;
            $month = $out[1];
            $day = $out[2];
            $hour = $out[3];
            $min = $out[5];
            $sec = '00';
            if(isset($out[7])) {
                $sec = $out[7];
            }
            $isodate = sprintf("%04d-%02d-%02d %02d:%02d:%02d", date("Y"), $month, $day, $hour, $min, $sec);
            $time = strtotime($isodate);
            //$time = strtotime(date("Y").'-'.$month.'-'.$day." {$hour}:{$min}:{$sec}");
            break;
        }
        //add end

        //第13层 ：处理 昨天 今天
        if( preg_match("/^(昨天|前天|今天)/", $str, $out) ) {
            $hour = 0;
            $min = 0;
            if ($hour >= 0 && $hour < 24 && $min >= 0 && $min < 60) {
                $exp_time = 60 * $min + 3600 * $hour;
                if( $out[1] == '昨天' ) {
                    $time = strtotime(date('Y-m-d' , strtotime('-1 day'))) + $exp_time;
                } elseif ($out[1] == '前天' ) {
                    $time = strtotime(date('Y-m-d' , strtotime('-2 days'))) + $exp_time;
                } elseif($out[1] == '今天') {
                    $time = strtotime(date('Y-m-d' , time())) + $exp_time;
                }
                break;
            }
        }
    } while (FALSE);

    if ($time > 0) {
        return 'timestamp'== $format ? $time : date($format, $time) ;
    } else {
        return FALSE;
    }
}


/**
 *
 * 根据URL获取数据
 * @author lgh
 * @param  $url
 * @return array()
 */
function get_from_curl($url='') {
    if( empty($url) ) return false;
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
    $ret = curl_exec($ch);
    return $ret ? $ret : @file_get_contents($url);
}

/**
 * 返回json格式数据
 * @param $msg
 * @param string $data
 */
function json_msg($state, $msg) {
    $rs['state'] = $state;
    $rs['msg'] = $msg;

    header('Content-type: application/json');
    echo json_encode($rs);
    exit;
}

function getCodeByMid($mid){
	$url = '';

	for ($i = strlen($mid) - 7; $i > -7; $i -=7)//从最后往前以7字节为一组读取mid
	{
		$offset1 = $i < 0 ? 0 : $i;
		$offset2 = $i + 7;
		$num = substr($mid, $offset1,$offset2-$offset1);
		//mid.substring(offset1, offset2);

		$num = int10to62($num);
		$url = $num .$url;
	}

	return $url;
}

function int10to62($int10) {
	$str62keys = array(
		"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
		"a", "b", "c", "d", "e", "f", "g", "h", "i",
		"j", "k", "l", "m", "n", "o", "p", "q",
		"r", "s", "t", "u", "v", "w", "x", "y",
		"z","A", "B", "C", "D", "E", "F", "G", "H",
		"I", "J", "K", "L", "M", "N", "O", "P",
		"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
	);
	
	$s62 = '';
	$r = 0;
	while ($int10 != 0)
	{
		$r = $int10 % 62;
		$s62 = $str62keys[$r].$s62;
		$int10 = floor($int10 / 62);
	}
	return $s62;
}
